import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold/common/index.dart';
import 'package:oramemo/common/langs/text_key.dart'; //
import 'package:oramemo/widget/my_refresh.dart';

import 'facility_logic.dart';
import 'view/conversation_item_widget.dart';
import 'view/device_widget.dart';

class FacilityPage extends StatelessWidget {
  const FacilityPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FacilityLogic>(
        init: FacilityLogic(),
        builder: (logic) {
          final calls = logic.recordController.callRecords;
          return Column(
            children: [
              // Device Info Card
              DeviceWidget(data: logic.state.data),
              // Message List
              Expanded(
                child: Container(
                  padding: const EdgeInsets.only(bottom: 18),
                  decoration: const BoxDecoration(
                    color: Color(0XFFF3F6F9),
                    borderRadius: BorderRadius.all(Radius.circular(22)),
                  ),
                  child: MyRefresh(
                    onRefresh: () async {
                      await logic.requestDevices();
                      await logic.requestCallRecords(page: 1);
                    },
                    onLoad: () async {},
                    child: (calls.isEmpty)
                        ? ListView(
                            children: [
                              Center(child: Text(TextKey.noCallRecords.tr)),
                            ],
                          )
                        : ListView.builder(
                            itemCount: calls.length,
                            padding: const EdgeInsets.symmetric(horizontal: 16),
                            itemBuilder: (context, index) {
                              final item = calls[index];
                              final message = Message(
                                id: item.id!,
                                type: item.conversationType == 1
                                    ? MessageType.incoming
                                    : MessageType.outgoing,
                                time: item.hangupTime!,
                                message: item.firstWord?.trim() ?? '',
                              );
                              return ConversationItemWidget(message: message);
                            },
                          ),
                  ),
                ),
              ),
            ],
          );
        });
  }
}
